package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbGoodsDescExample;
import com.pyg.pojo.TbGoodsDescExample.Criteria;
import com.pyg.mapper.TbGoodsDescMapper;
import com.pyg.pojo.TbGoodsDesc;
import com.pyg.goods.service.TbGoodsDescService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbGoodsDescServiceImpl implements TbGoodsDescService {
	@Autowired
	private TbGoodsDescMapper tbGoodsDescMapper;

	@Override
	public List<TbGoodsDesc> findAll() {
		return tbGoodsDescMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbGoodsDesc> page = (Page<TbGoodsDesc>) tbGoodsDescMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbGoodsDesc
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbGoodsDesc tbGoodsDesc, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbGoodsDescExample example = null;
		if (tbGoodsDesc != null) {
			example = new TbGoodsDescExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getGoodsId())) {
				criteria.andGoodsIdEqualTo(tbGoodsDesc.getGoodsId() );
			}
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getIntroduction())) {
				criteria.andIntroductionLike("%" + tbGoodsDesc.getIntroduction() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getSpecificationItems())) {
				criteria.andSpecificationItemsLike("%" + tbGoodsDesc.getSpecificationItems() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getCustomAttributeItems())) {
				criteria.andCustomAttributeItemsLike("%" + tbGoodsDesc.getCustomAttributeItems() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getItemImages())) {
				criteria.andItemImagesLike("%" + tbGoodsDesc.getItemImages() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getPackageList())) {
				criteria.andPackageListLike("%" + tbGoodsDesc.getPackageList() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoodsDesc.getSaleService())) {
				criteria.andSaleServiceLike("%" + tbGoodsDesc.getSaleService() + "%");
			}
		}

		Page<TbGoodsDesc> page = (Page<TbGoodsDesc>) tbGoodsDescMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbGoodsDesc tbGoodsDesc) {
		tbGoodsDescMapper.insert(tbGoodsDesc);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbGoodsDescMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbGoodsDesc findOne(Long id) {
		return tbGoodsDescMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbGoodsDesc tbGoodsDesc) {
		tbGoodsDescMapper.updateByPrimaryKey(tbGoodsDesc);
	}

}
